package cn.itcast.bos.web.action.take_delivery;

import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import cn.itcast.bos.web.action.common.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * 表现层
 * @author: SOS
 * @create: 2018-03-06 11:31
 */

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class WayBillAction extends BaseAction<WayBill> {


    @Autowired
    private WayBillService wayBillService;

    @Action(value = "waybill_save",results = {@Result(name = SUCCESS,type = "json"),
            @Result(name = "input",location = "./index.html",type = "redirect")})
    public String wayBillSave(){

        if (model.getOrder() != null && (model.getOrder().getId() == null || model.getOrder().getId() == 0)) {
            model.setOrder(null);
        }

        //将内容转化为json返回
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            map.put("success",true);
            map.put("msg","保存成功....");
            wayBillService.save(model);
            ActionContext.getContext().getValueStack().push(map);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","警告保存失败");
        }


        return SUCCESS;
    }


    //分页列表查询

    @Action(value = "waybill_pageQuery",results = {@Result(name = SUCCESS,type = "json"),
            @Result(name = "input",location = "./index.html",type = "redirect")})
    public String wayBillQuery(){

        Pageable pageable = new PageRequest(page-1,rows,new Sort(new Sort.Order(Sort.Direction.DESC,"id")));

        Page<WayBill> pageData = wayBillService.findPageData(model,pageable);
        pushPageDataToValueStack(pageData);
        return SUCCESS;
    }

    //根据waybillNum查询数据

    @Action(value = "wayBill_findByWayBillNum",results = {@Result(name = SUCCESS,type = "json"),
            @Result(name = "input",location = "./index.html",type = "redirect")})
    public String findByWayBillNum(){

        Map<String,Object> map = new HashMap<String, Object>();
        try {
            WayBill wayBill = wayBillService.findByWayBillNum(model.getWayBillNum());
            map.put("success",true);
            map.put("wayBillData",wayBill);
        } catch (Exception e) {
            map.put("success",false);
            e.printStackTrace();
        }

        ActionContext.getContext().getValueStack().push(map);
        return SUCCESS;
    }



}
